1 /**
2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module config;
7 
8 public import core.stdc.stdlib;
9 public import std.algorithm;
10 public import std.array;
11 public import std.ascii;
12 public import std.conv;
13 public import std.file;
14 public import std.process;
15 public import std.path;
16 public import std.range;
17 public import std.stdio;
18 public import std..string;
19 public import logger = std.experimental.logger;
20 
21 public import unit_threaded.light;
22 
23 immutable string codeCherckerBin;
24 immutable compileCommandsFile = "compile_commands.json";
25 immutable testData = "testdata";
26 immutable tmpDir = "./build/test_area";
27 
28 shared static this() {
29     codeCherckerBin = absolutePath("../build/code_checker");
30 }
31 
32 struct TestArea {
33     const string workdir;
34 
35     alias workdir this;
36 
37     this(string file, ulong id) {
38         this.workdir = buildPath(tmpDir, file ~ id.to!string).absolutePath;
39         setup();
40     }
41 
42     void setup() {
43         if (exists(workdir)) {
44             rmdirRecurse(workdir);
45         }
46         mkdirRecurse(workdir);
47     }
48 }
49 
50 struct RunResult {
51     int status;
52     string[] stdout;
53     string[] stderr;
54 
55     void print() {
56         import std.ascii : newline;
57 
58         writeln("stdout: ", stdout.joiner(newline));
59         writeln("stderr: ", stderr.joiner(newline));
60     }
61 }
62 
63 RunResult run(string[] cmd, string workdir = null) {
64     import std.array : appender;
65     import std.algorithm : joiner, copy;
66     import std.ascii : newline;
67     import std.process : pipeProcess, tryWait, Redirect;
68     import std.stdio : writeln;
69     import core.thread : Thread;
70     import core.time : dur;
71 
72     logger.trace("run: ", cmd.joiner(" "));
73     if (workdir !is null)
74         logger.trace("workdir is ", workdir);
75 
76     auto app_out = appender!(string[])();
77     auto app_err = appender!(string[])();
78 
79     auto p = pipeProcess(cmd, Redirect.all, null, Config.none, workdir);
80     int exit_status = -1;
81 
82     while (true) {
83         auto pres = p.pid.tryWait;
84 
85         p.stdout.byLineCopy.copy(app_out);
86         p.stderr.byLineCopy.copy(app_err);
87 
88         if (pres.terminated) {
89             exit_status = pres.status;
90             break;
91         }
92 
93         Thread.sleep(25.dur!"msecs");
94     }
95 
96     return RunResult(exit_status, app_out.data, app_err.data);
97 }